package com.cashmate.struts;

import com.cashmate.hibernate.dao.CharacterreferenceDAO;
import com.cashmate.hibernate.pojo.Characterreferences;
import com.cashmate.hibernate.pojo.Employees;
import com.cashmate.hibernate.pojo.Usertypes;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.ServletActionContext;

public class EditCharref extends ActionSupport {

    private static Logger logger = Logger.getLogger(EditCharref.class);
    //This properties have setters below... The setters are called and populated by the grid.
    private String id;//the column in grid which we set key=true
    private String name;
    private String address;
    private String occupation;
    private CharacterreferenceDAO cdao = (CharacterreferenceDAO) ServletActionContext.getServletContext().getAttribute("characterreferenceDAO");
    private Employees emps = (Employees) ServletActionContext.getRequest().getSession().getAttribute("employee");
    private Usertypes ut = (Usertypes) ServletActionContext.getRequest().getSession().getAttribute("usertype");

    //Notice we don't have a success result because we do not need to redirect the user when he/she updates the dbase
    //We redirect him/her only if an error occurred.
    @Action(value = "/edit-charref", results = {
        @Result(name = "error", type = "redirectAction", location = "../error/messages.jsp")
    })
    @Override
    public String execute() throws Exception {
        Characterreferences c = cdao.get(Integer.parseInt(id));
        try {
            String fieldname = "";
            String fieldvalue = "";
            if (name != null) {
                c.setName(name);
                fieldname = "name";
                fieldvalue = name;
            }
            if (address != null) {
                c.setAddress(address);
                fieldname = "address";
                fieldvalue = address;
            }
            if (occupation != null) {
                c.setOccupation(occupation);
                fieldname = "occupation";
                fieldvalue = occupation;
            }
            cdao.update(c);
            logger.info(ut.getType() + " (" + emps.getIdnumber() + ") updated Characterreferences(Series #" + c.getCharrefid() + ") with value: " + fieldname + ":" + fieldvalue);
        } catch (Exception e) {
            if (emps != null) {
                logger.fatal("(" + emps.getIdnumber() + "):" + e.getLocalizedMessage(), e);
            } else {
                logger.fatal(e.getLocalizedMessage(), e);
                e.printStackTrace();
            }
            addActionError(e.getLocalizedMessage());
            return ERROR;
        }
        return NONE;
    }
    //setters

    public void setAddress(String address) {
        this.address = address;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setOccupation(String occupation) {
        this.occupation = occupation;
    }
}
